package com.guigu.pd.base.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.base.common.CustomIdGenerator;
import com.guigu.pd.base.entity.*;
import com.guigu.pd.base.service.TruckDriverService;
import com.guigu.pd.base.service.TruckService;
import com.guigu.pd.base.service.TruckTypeService;
import com.guigu.pd.serviceapi.authfeign.CoreOrgFeign;
import com.guigu.pd.serviceapi.authfeign.UserFeign;
import feign.Feign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.base.service.FleetService;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.R;



/**
 * 车队表
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:09:01
 */
@Api(tags = "车队管理")
@RestController
@RequestMapping("base/fleet")
public class FleetController {
    @Autowired
    private FleetService fleetService;

    @Autowired
    private TruckService truckService;

    @Autowired
    private TruckDriverService truckDriverService;


    @Autowired
    private TruckTypeService truckTypeService;


    @Autowired
    private UserFeign userFeign;


    @Autowired
    private CoreOrgFeign coreOrgFeign;

    @Autowired
    private CustomIdGenerator customIdGenerator;


    /**
     * 分页查询数据
     * @param
     * @return
     */
    @ApiOperation("分页条件查询车队数据")
    @GetMapping("/queryfleettypefy")
    public R queryfleettypefy(@RequestParam Map<String, Object> params){
        PageVo<FleetEntity> page = fleetService.queryfleettypefy(params);
        return R.ok().put("page",page);
    }


    /*/**
     * 删除车队类型--逻辑删除
     *
     * @param id 车队id
     * @return 返回信息
     */
    @ApiOperation("删除车队类型--逻辑删除")
    @DeleteMapping("/delectfleetbyid/{id}")
    public R delectfleetbyid(@PathVariable(name = "id") String id) {

        boolean b = fleetService.removeById(id);
        if(b){
            return R.ok("删除成功");
        }
        return R.ok("删除失败");

    }



    /**
     * 根据id获取车次详情
     *
     * @param id 车次id
     * @return 车次信息
     */
    @ApiOperation("根据id获取车次详情")
    @GetMapping("/finefleetById/{id}")
    public FleetEntity finefleetById(@PathVariable(name = "id") String id) {
        FleetEntity fleetEntity = fleetService.getById(id);
        return fleetEntity;
    }


    /**
     * 更新车次信息
     *
     * @param fleetEntity 车队信息
     * @return 车次信息
     */
    @ApiOperation("更新车次信息")
    @PutMapping("/updatefleetbyid")
    public R updatefleetbyid(@RequestBody FleetEntity fleetEntity) {

        boolean b = fleetService.updateById(fleetEntity);

        if(b){
            return R.ok("修改成功");
        }
        return R.ok("修改失败");
    }




    /**
     * 查询全部车队以及下属车辆
     */
    @ApiOperation("查询全部车队以及下属车辆")
    @GetMapping("/selectfleetandtruck")
    public List<FleetEntity> selectfleetandtruck(){


        List<FleetEntity> list = fleetService.list();

        for (FleetEntity fleetEntity : list) {

            QueryWrapper<TruckEntity> queryWrapper = new QueryWrapper<TruckEntity>();

            queryWrapper.eq("fleet_id",fleetEntity.getId());
            List<TruckEntity> list1 = truckService.list(queryWrapper);

            fleetEntity.setTruckEntities(list1);
        }

        return list;
    }


    /**
     * 根据id查询车队下属车辆
     */
    @ApiOperation("根据id查询车队下属车辆")
    @GetMapping("/selecttruckbyfleetid/{id}")
    public List<TruckEntity> selecttruckbyfleetid(@PathVariable(name = "id") String id){

        QueryWrapper<TruckEntity> queryWrapper = new QueryWrapper<TruckEntity>();

        queryWrapper.eq("fleet_id",id);

        List<TruckEntity> list = truckService.list(queryWrapper);

        for (TruckEntity truckEntity : list) {

            TruckTypeEntity byId = truckTypeService.getById(truckEntity.getTruckTypeId());

            truckEntity.setTruckTypename(byId.getName());

        }


        return list;
    }


    /**
     * 查询全部车队以及下属司机
     */
    @ApiOperation("查询全部车队以及下属司机")
    @GetMapping("/selectfleetandtruckdriver/")
    public List<FleetEntity> selectfleetandtruckdriver(){
        List<FleetEntity> list = fleetService.list();

        for (FleetEntity fleetEntity : list) {

            QueryWrapper<TruckDriverEntity> queryWrapper = new QueryWrapper<TruckDriverEntity>();

            queryWrapper.eq("fleet_id",fleetEntity.getId());

            List<TruckDriverEntity> list1 = truckDriverService.list(queryWrapper);

            //获取到司机根据编号去用户表查询信息
            for (TruckDriverEntity truckDriverEntity : list1) {

                String getnamebyid = userFeign.getnamebyid(truckDriverEntity.getUserId());
                truckDriverEntity.setName(getnamebyid);
            }

            fleetEntity.setDriverEntities(list1);

        }

        return list;
    }


    /**
     * 根据车队id查询车队下属司机
     */
    @ApiOperation("根据id查询车队下属司机")
    @GetMapping("/selectTruckDriverbyfleetid/{id}")
    public List<TruckDriverEntity>  selectfleetandtruckdriver(@PathVariable(name = "id") String id){



            QueryWrapper<TruckDriverEntity> queryWrapper = new QueryWrapper<TruckDriverEntity>();

            queryWrapper.eq("fleet_id",id);

            List<TruckDriverEntity> list1 = truckDriverService.list(queryWrapper);

            //获取到司机根据编号去用户表查询信息
            for (TruckDriverEntity truckDriverEntity : list1) {

                //获取司机名称
                String getnamebyid = userFeign.getnamebyid(truckDriverEntity.getUserId());

                String[] str =  getnamebyid.split(",");

                //获取司机所在机构名称
                String getcornamebyid = coreOrgFeign.getcornamebyid(str[1]);

                truckDriverEntity.setName(str[0]);
                truckDriverEntity.setCoreOrgname(getcornamebyid);
            }

        return list1;
    }


    /**
     * 获取车队列表
     */
    @ApiOperation("获取车队列表")
    @GetMapping("/findfleetAll")
    public List<FleetEntity> findfleetAll() {

        return fleetService.list();
    }

    /**
     * 添加车队
     */
    @ApiOperation("添加车队")
    @PostMapping("/savefleet")
    public R queryfleettypefy(@RequestBody FleetEntity fleetEntity){

        fleetEntity.setId(customIdGenerator.nextId(fleetEntity)+"");


        boolean save = fleetService.save(fleetEntity);

        if(save){
            return R.ok("添加成功");
        }

        return R.ok("添加失败");
    }

    @RequestMapping("/getbyId/{id}")
    //@RequiresPermissions("base:fleet:info")
    public String getfleetbyId(@PathVariable("id") String id){
        FleetEntity fleet = fleetService.getById(id);
        return fleet.getAgencyId();
    }


    @GetMapping("/getfleetnamebyId/{id}")
    public String getfleetnamebyId(@PathVariable("id") String id){
        FleetEntity fleet = fleetService.getById(id);
        return fleet.getName();
    }


}
